//
//  PersonUserInfoTableViewController.m
//  LPTClient
//
//  Created by apple on 2018/12/7.
//  Copyright © 2018 zhaodd. All rights reserved.
//

#import "PersonUserInfoTableViewController.h"
#import "DoctorBarCodeViewController.h"
#import "LPTForgotPassViewController.h"
#import "PersonUserInfoTableViewCell.h"

@interface PersonUserInfoTableViewController ()

@property (nonatomic, strong) NSArray *m_array;

@end

@implementation PersonUserInfoTableViewController


+ (instancetype)instance
{
    UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Mine" bundle:nil];
    PersonUserInfoTableViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"PersonUserInfoTableViewController"];
    return vc;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.m_array = @[
                     @[
                         @{ @"title" : @"头像" , @"hasDetail" : @(1)}
                         ],
                     @[
                         @{@"title" : @"姓名", @"hasDetail" : @(0)},
                         @{@"title" : @"性别", @"hasDetail" : @(0)},
                         @{@"title" : @"手机号", @"hasDetail" : @(0)}
                         ],
                     @[
                         @{@"title" : @"我的二维码", @"hasDetail" : @(1)},
                         @{@"title" : @"修改密码", @"hasDetail" : @(1)}
                         ]
                     ];
    
    
    UIButton *commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    commitBtn.frame = CGRectMake(15, 35, kScreenWidth - 30, 45);
    commitBtn.backgroundColor = [UIColor whiteColor];
    [commitBtn.layer setMasksToBounds:YES];
    [commitBtn.layer setCornerRadius:22.5];
    [commitBtn setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"#F7941E"]] forState:UIControlStateNormal];
    [commitBtn addTarget:self action:@selector(commitButton) forControlEvents:UIControlEventTouchUpInside];
    [commitBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    [self.view addSubview:commitBtn];
    
    UIView *v = [[UIView alloc] init];
    v.frame = CGRectMake(0, 0, kScreenWidth, 90);
    [v addSubview:commitBtn];
    
    self.tableView.tableFooterView = v;
    
}

- (void)commitButton
{
    
    UIAlertController *alerVC = [UIAlertController alertControllerWithTitle:@"退出登录" message:@"确定要退出登录吗？" preferredStyle:UIAlertControllerStyleAlert];
    [alerVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }]];
    [alerVC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

        [self.navigationController popToRootViewControllerAnimated:NO];
        [[LTPCacheManager shareInstance] logOut];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"auto_present_loginvc" object:nil];

    }]];
    [self presentViewController:alerVC animated:YES completion:NULL];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.m_array.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return ((NSArray *)self.m_array[section]).count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 80;
    }
    return 45;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    PersonUserInfoTableViewCell *cell = (PersonUserInfoTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"PersonUserInfoTableViewControllerID" forIndexPath:indexPath];

    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    
    NSDictionary *dict = self.m_array[section][row];
    NSString *title = dict[@"title"];
    NSInteger hasDetail = [dict[@"hasDetail"] integerValue];
    
    cell.titleLab.text = title;
    cell.accessoryType = (hasDetail == 1)?UITableViewCellAccessoryDisclosureIndicator:UITableViewCellAccessoryNone;
    
    //  处理数据
    UserInfo *userInfo = [LTPCacheManager shareInstance].getUserInfoModel.doctor;
    cell.iconImageView.hidden = YES;
    
    if (section==0 && row == 0) {// 头像
        cell.iconImageView.hidden = NO;
        NSString *url = userInfo.credential.figure_file.url;
        [cell.iconImageView setImageWithURL:[NSURL URLWithString:IMG_URL(url)] placeholder:[UIImage imageNamed:@"my_nav_portrait"]];
    }
    
    if (section==2 && row == 0) {// 二维码
        cell.iconImageView.hidden = NO;
        cell.iconImageView.image = [UIImage imageNamed:@"data_icon_code"];
    }

    if (userInfo && section == 1) {
        if (row == 0) {
            NSString *name = userInfo.credential.name;
            cell.descLab.text = name;
        }else if (row == 1) {
            NSString *sex = @"";
            if (userInfo.credential.sex == 1) {
                sex = @"男";
            }else if (userInfo.credential.sex == 2) {
                sex = @"女";
            }
            cell.descLab.text = sex;
        }else if (row == 2) {
            NSString *phone = userInfo.mobile;
            cell.descLab.text = phone;
        }
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    
    if (section == 0 && row == 0) {
        // 换头像
        
    }else if (section == 2) {
        if (row == 0) {
            // 二维码
            DoctorBarCodeViewController *vc = [DoctorBarCodeViewController instance];
            [self.navigationController pushViewController:vc animated:YES];

        }else if (row == 1) {
            // 修改密码
            LPTForgotPassViewController *vc = [LPTForgotPassViewController instance];
            [vc isResetPasswordVC:YES];
            [self.navigationController pushViewController:vc animated:YES];

        }
    }
}

@end
